/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-2
*
\*********************************************************************/
#include <thread>

#include "CamGainControl.h"
#include "I2cUserspace.h"
#include "UtilityTH.h"


CamGainControl CamGainControl::m_Instance;

CamGainControl::CamGainControl()
{
}

CamGainControl& CamGainControl::GetInstance()
{
    return m_Instance;
}

void CamGainControl::Init()
{
    //GainChipReset();
    SetGain(CAM_GAIN_INIT);
    SetGainAutoMode(false);
}

void CamGainControl::IncGain(int step)
{
    SetGain(m_camGain+step);
}

void CamGainControl::DecGain(int step)
{
    SetGain(m_camGain-step);;
}

void CamGainControl::SetGain(int value)
{
    if(value>CAM_GAIN_MAX)
    {
        m_camGain = CAM_GAIN_MAX;
    }
    else if(value<CAM_GAIN_MIN)
    {
        m_camGain = CAM_GAIN_MIN;
    }
    else
    {
        m_camGain = value;
    }
    long i2cGainData =long(MIN_IIC_GAIN_DATA+MIN_DATA_STEP*m_camGain);//0xFFFF对应增益5V，0x0000对应增益0V，0x9999对应增益3V。0x9999时，程序内部gain为0，对应增益3V。
    if(i2cGainData>MAX_IIC_GAIN_DATA)
    {
        i2cGainData = MAX_IIC_GAIN_DATA;
    }
    unsigned char high = (i2cGainData>>8)&0xff;
    unsigned char low = i2cGainData&0xff;
    float voltage = ZERO_GAIN_VOLTAGE+VOLTAGE_STEP*m_camGain;
    DebugPrintf("CamGain iic data high %#X low %#X,CamGain is %d,voltage is %.3fv\n",high,low,value,voltage);
    DebugPrintf("now set camera gain\n");
    CamGainIicSend(high,low);
}

int CamGainControl::GetGain()
{
    return m_camGain;
}

void CamGainControl::SetGainAutoMode(bool isAuto)
{
    m_gainIsAuto = isAuto;
}

bool CamGainControl::GetCamGainMode()
{
    return m_gainIsAuto;
}

//void CamGainControl::GainChipReset()
//{
////    GpioManage::GetInstance().IoWrite(GpioPinNum::AD5693_ADDR_PROG,IoValue::LOW);
////    GpioManage::GetInstance().IoWrite(GpioPinNum::AD5693_GAIN_RESET,IoValue::HIGH);
////    std::this_thread::sleep_for(std::chrono::milliseconds(10));
////    GpioManage::GetInstance().IoWrite(GpioPinNum::AD5693_GAIN_RESET,IoValue::LOW);
////    std::this_thread::sleep_for(std::chrono::milliseconds(10));
//}

//void CamGainControl::GainChipNormal()
//{
////    GpioManage::GetInstance().IoWrite(GpioPinNum::AD5693_GAIN_RESET,IoValue::LOW);
////    GpioManage::GetInstance().IoWrite(GpioPinNum::AD5693_ADDR_PROG,IoValue::LOW);
//}

void CamGainControl::CamGainIicSend(unsigned char high, unsigned char low)
{
    unsigned char addr = GAIN_IIC_ADDR;
    int len = GAIN_IIC_DATA_LEN;
    unsigned char data[] = {0x40, 0x08, 0x00};
    I2csend(addr,len,data);
    data[0] = 0x10;
    data[1] = high;
    data[2] = low;
    I2csend(addr,len,data);
}


/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/



